package com.example.emos.api.controller;

import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.dao.TbEvaluationDao;
import com.example.emos.api.db.pojo.TbExamEva;
import com.example.emos.api.db.pojo.TbExamEvaGroup;
import com.example.emos.api.service.EvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author ：Supyo
 * @date ：Created in 2021/11/16 14:41
 * @description：
 */
@RestController
@RequestMapping("/evaluation")
@Tag(name = "EvaluationController", description = "评分指标库接口")
@Slf4j
public class EvaluationController {
    @Autowired
    TbEvaluationDao evaluationDao;
    @Autowired
    private EvaluationService evaluationService;

    @PostMapping("/selectEvaluationGroup")
    @Operation(summary = "指标组分页查询")
    public R selectEvaluationGroup(@Valid @RequestBody SelectEvaluationGroupForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        PageUtils pageUtils = evaluationService.selectEvaluationGroup(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/addEvaluationGroup")
    @Operation(summary = "新增指标组")
    public R addEvaluationGroup(@Valid @RequestBody InsertEvaluationGroupForm form) {
        TbExamEvaGroup evaGroup = JSONUtil.parse(form).toBean(TbExamEvaGroup.class);
        int rows = evaluationService.insert(evaGroup);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/updateEvaluationGroup")
    @Operation(summary = "修改指标组")
    public R updateEvaluationGroup(@Valid @RequestBody UpdateEvaluationGroupForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        int rows = evaluationService.updateEvaluationGroup(param);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteEvaluationGroup")
    @Operation(summary = "删除指标组")
    public R deleteEvaluationGroup(@Valid @RequestBody DeleteEvaluationGroupForm form) {
        int rows = evaluationService.deleteEvaluationGroup(form.getIds());
        return R.ok().put("rows", rows);
    }


    //====================================================================
    //=====================================================================
    //=====================================================================

    @PostMapping("/selectCheckListById")
    @Operation(summary = "根据Id查询指标项")
    public R selectEvaluationById(@Validated @RequestBody SelectEvaluationByIdForm form) {
        TbExamEva eva = evaluationService.selectEvaluationById(form.getId());
        return R.ok().put("data", eva);
    }

    @PostMapping("/selectEvaluationsBypages")
    @Operation(summary = "指标项分页查询")
    public R selectEvaluationsBypages(@Valid @RequestBody SelectEvaluationsBypagesForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        PageUtils pageUtils = evaluationService.selectEvaluationsBypages(param);
        return R.ok().put("page", pageUtils);
    }


    @PostMapping("/addEvaluations")
    @Operation(summary = "新增指标")
    public R addEvaluations(@Valid @RequestBody InsertEvaluationsForm form) {
        TbExamEva eva = JSONUtil.parse(form).toBean(TbExamEva.class);
        int rows = evaluationService.addEvaluations(eva);
        int groupId = form.getGroupId();
        evaluationDao.updateCount(groupId);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/updateEvaluations")
    @Operation(summary = "修改指标")
    public R updateEvaluations(@Valid @RequestBody UpdateEvaluationsForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        int rows = evaluationService.updateEvaluations(param);
        //更新数量
        int oldgroupId = form.getOldGroupId();
        int newgroupId = form.getGroupId();
        if (newgroupId != 0) {
            evaluationDao.updateCount(newgroupId);
        }
        if (oldgroupId != 0) {
            evaluationDao.updateCount(oldgroupId);
        }
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteEvaluations")
    @Operation(summary = "删除指标")
    public R deleteEvaluations(@Valid @RequestBody DeleteEvaluationsForm form) {
        int rows = evaluationService.deleteEvaluations(form.getIds());
        int groupId[] = evaluationDao.searchgrupId();
        for (int i = 0; i < groupId.length; i++) {
            int rows1 = evaluationDao.updategroupId(groupId[i]);
        }
        return R.ok().put("rows", rows);
    }

    @PostMapping("/moveEvaluations")
    @Operation(summary = "移动指标")
    public R moveEvaluations(@Valid @RequestBody MoveEvaluationsForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        int rows = evaluationService.moveEvaluations(param);
        int oldgroupId = form.getOldgroupId();
        int newgroupId = form.getNewgroupId();
        if (newgroupId != 0) {
            evaluationDao.updateCount(newgroupId);
        }
        if (oldgroupId != 0) {
            evaluationDao.updateCount(oldgroupId);
        }
        return R.ok().put("rows", rows);
    }
}
